# -*- coding: utf-8 -*-
__author__ = 'lztkdr'
__date__ = '2020/12/3 9:03'

"""
    使用第三方的greenlet来实现协程
    1- 协程保存当前的执行环境比如变量等，在切换之前
    2- 恢复之前的执行环境，接着上次的继续执行
"""

import time
from greenlet import greenlet


def work1(s1, s2):
	res = g2.switch(s1 + s2)
	print('work1:', res)


def work2(ss):
	print('work2:', ss)
	# 手动切换到g1执行，底层封装了yield
	res = g1.switch('c')


# 创建两个协程对象
g1 = greenlet(work1)
g2 = greenlet(work2)


def testGreenLet():
	# 切换到协程执行,就开始执行work1，因为work1函数中调用了g2.switch()实现来回切换
	g1.switch('a', 'b')


if __name__ == "__main__":
	testGreenLet()
